﻿//using DocumentFormat.OpenXml;
//using DocumentFormat.OpenXml.Packaging;
//using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.ExtendedProperties;
using HtmlToOpenXml;
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DocxUtil
{
    internal class Program
    {
        static void CountChars(string docxFile)
        {
            Application application = new Application();
            Document doc = application.Documents.Open(docxFile);
            object includeFootnotesAndEndnotes = false;
            Microsoft.Office.Interop.Word.WdStatistic stats = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticCharacters;
            int wordCount = doc.ComputeStatistics(stats, ref includeFootnotesAndEndnotes);
            Console.WriteLine(wordCount);
        }



        static void InteropHtml2Docx()
        {
            //const string filename = "D:\\dl\\imported.docx";
            const string filename = "C:\\Users\\yang.lei\\Downloads\\imported.docx";
            //string html = File.ReadAllText(@"D:\dl\htmls\2653.html");
            //string html = @"D:\dl\htmls\2653.html";
            string html = @"C:\\Users\\yang.lei\\Downloads\\htmls\2653.html";
            //string html = @"C:\\Users\\yang.lei\\Downloads\\in.html";
            //string html = File.ReadAllText(@"C:\\Users\\yang.lei\\Downloads\\in.html");

            if (File.Exists(filename)) File.Delete(filename);
            var word = new Microsoft.Office.Interop.Word.Application();
            var wordDoc = word.Documents.Open(FileName: html, ReadOnly: false);
            wordDoc.SaveAs2(FileName: filename, FileFormat: WdSaveFormat.wdFormatXMLDocument);
            wordDoc.Close();
        }
        static void Main(string[] args)
        {
            //CountChars("D:\\dl\\docs\\2653.docx");
            //CountChars("C:\\Users\\yang.lei\\Downloads\\docs\\2653.docx");
            InteropHtml2Docx();
            Console.ReadKey();
        }
    }
}
